package com.situ.web.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.situ.web.entity.PageInfo;
import com.situ.web.entity.Student;
import com.situ.web.entity.User;
import com.situ.web.service.StudentService;
import com.situ.web.util.JDBCUtil;

@WebServlet("/student")
public class StudentServlet extends HttpServlet{
	private StudentService studentService = new StudentService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 解决POST传参乱码问题,一定要放在所有getParameter之前
		req.setCharacterEncoding("UTF-8");
		
		// 判断当前用户是否已经登录
//		HttpSession session = req.getSession();
//		User user = (User)session.getAttribute("user");
//		if (user == null) {// 用户没有登录，重定向到登录界面
//			resp.sendRedirect(req.getContextPath() + "/user_login.jsp");
//			return;
//		}
		
		
		// http://localhost:8080/Java1912Web/deleteStudent?id=12
		// http://localhost:8080/Java1912Web/student?method=deleteById&id=12
		// http://localhost:8080/Java1912Web/student?method=findAll
		// http://localhost:8080/Java1912Web/student?method=findByPage
		String method = req.getParameter("method");
		
		// 如果没有传递method参数，默认执行查找所有
		if (method == null || "".equals(method)) {
			//method = "findAll";
			method = "findByPage";
		}
		switch (method) {
		case "findByPage":
			findByPage(req, resp);
			break;
		case "findAll":
			findAll(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "selectById":
			selectById(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "checkName":
			checkName(req, resp);
			break;
		default:
			break;
		}
	}

	private void checkName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String name = req.getParameter("name");
		boolean isExist = studentService.checkName(name);
		
		// 通过response告诉浏览器以什么样的编码来解析这个数据
		resp.setContentType("text/html;charset=utf-8");
		// {"isExist": isExist}
		resp.getWriter().write("{\"isExist\": " + isExist + "}");
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// /Java1912Web2/student?method=findByPage&pageNo=2&pageSize=5
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		// 如果pageNo和pageSize没有传递，设置默认值pageNo=1,pageSize=5
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = studentService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/student_page_list.jsp").forward(req, resp);
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		Student student = new Student(Integer.parseInt(id), name, Integer.parseInt(age), gender);
		System.out.println(student);
		
		boolean isSuccess = studentService.update(student);
		
		if (isSuccess) {
			// 修改完之后，重定向到/Java1912Web/findAllStudent
			resp.sendRedirect(req.getContextPath() + "/student?method=finByPage");
		} else {
			resp.sendRedirect(req.getContextPath() + "/error.html");
		}
		
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String selectId = req.getParameter("id");
		
		Student student = studentService.selectById(Integer.parseInt(selectId));
		
		req.setAttribute("student", student);
		
		req.getRequestDispatcher("/student_update.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// http://localhost:8080/Java1912Web/insertStudent?name=赵六&age=23&gender=男
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String gender = req.getParameter("gender");
		System.out.println(name);
		System.out.println(age);
		System.out.println(gender);
		Student student = new Student(name, Integer.parseInt(age), gender);
		
		boolean isSuccess = studentService.insert(student);
		
		// 添加完之后再到查询并展示所有的界面
		// http://localhost:8080/Java1912Web/findAllStudent
		resp.sendRedirect(req.getContextPath() + "/student?mtehod=findByPage");
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// http://localhost:8080/Java1912Web/student?method=deleteById&id=12
		// ?后面的id是传递的参数
		String id = req.getParameter("id");
		System.out.println("id:" + id);
		
		boolean isSuccess = studentService.deleteById(Integer.parseInt(id));
		
		// 删除完成之后，应该查询所有学生展示，就是请求 /Java1912Web/findAllStudent
		// resp.sendRedirect("/Java1912Web/findAllStudent");
		// req.getContextPath() 返回当前项目名字
		resp.sendRedirect(req.getContextPath() + "/student?method=findByPage");
	}

	private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Student> list = studentService.findAll();
		
		for (Student student : list) {
			System.out.println(student);
		}
		
		// 把查询出来的list集合放到req里面
		req.setAttribute("list", list);
		// 然后转发到student_list.jsp这个页面进行展示
		req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
	}
	
}
